var markit_comments = new HashTable();
var markit_oldComments = new HashTable();

function MarkComment() {
	this.time = "";
	this.cmnt = "";
	this.usr = "";
	this.id = -1;
	this.inited = false;
	this.status = 0;
	// 1:new, 2:dirty
}

var commentCount = 0;
function getNewCommentIndex() {
	return ++commentCount;
}

function commentIt(isNew, index) {
	var txt = getSelectionText();
	if (txt == "" || txt == undefined)
		return;
	document.body.setAttribute("contentEditable", true);
	if (!index)
		index = getNewCommentIndex();
	var baseUrl = "http://10.131.252.41:8080/edu.fudan.ss.wml.marker.test";
	if (window.attachEvent) {
		var pos = document.selection.createRange();
		if (pos.htmlText.length > 0) {
			pos.pasteHTML(pos.htmlText
					+ "<img class='iconComment' id='iconComment" + index
					+ "' onmousedown='showComment(" + index + ")' src='"
					+ baseUrl + "/icon/Comment.png'>");
		}
	} else {
		var range = window.getSelection().getRangeAt(0);
		var img = document.createElement("img");
		img.setAttribute("src", baseUrl + "/icon/Comment.png");
		img.setAttribute("class", "iconComment");
		img.setAttribute("id", "iconComment" + index);
		img.setAttribute("onmousedown", "showComment(" + index + ")");
		insertNode(range, img);
	}
	var mark = {
		'tp' : "Comment",
		'txt' : getSelectionText(),
		'v' : index
	};
	if (isNew == 1) {
		document.body.setAttribute("contentEditable", false);
		var cmnt = new MarkComment();
		cmnt.cmnt = "no Comment";
		cmnt.usr = "WML";
		cmnt.id = index;
		cmnt.time = getCurrentTime();
		cmnt.status = 1;
		addComment(index, cmnt);
		markit_markData.data.push(mark);
	}
}

function insertNode(range, node) {
	var sc = range.endContainer, so = range.endOffset, p = sc.parentNode, ns = sc.nextSibling, nn, cn;
	// 如果节点是 TEXT_NODE 或者 CDATA_SECTION_NODE
	if ((sc.nodeType === 3 || sc.nodeType === 4 || sc.nodeType === 8)
			&& sc.nodeValue) {
		if (so === 0) {
			// At the start of text
			p.insertBefore(node, sc);
		} else if (so >= sc.nodeValue.length) {
			// At the end of text
			if (ns) {
				p.insertBefore(node, ns);
			} else {
				p.appendChild(node);
			}
		} else {
			// Middle, need to split
			nn = sc.splitText(so);
			p.insertBefore(node, nn);
		}
	} else {
		cn = "";
		if (sc.childNodes.length > 0) {
			cn = sc.childNodes[so];
		}
		if (cn) {
			sc.insertBefore(node, cn);
		} else {
			sc.appendChild(node);
		}
	}
}

function getCommentIconX(id) {
	return document.getElementById(id).offsetLeft + 2;
}
function getCommentIconY(id) {
	return document.getElementById(id).offsetTop + 30;
}

function showComment(index) {
	if (!isInited(index)) {
		init(index);
	} else {
		var cp = document.getElementById("outDivComment" + index);
		cp.style.top = getCommentIconY("iconComment" + index) + "px";
		cp.style.left = getCommentIconX("iconComment" + index) + "px";
		cp.style.display = "block";
	}
}

function hideComment(index) {
	var cp = document.getElementById("outDivComment" + index);
	cp.style.display = "none";
}

function addComment(index, cmnt) {
	markit_comments.put("" + index, cmnt);
}

function isInited(index) {
	var cmnt = getComment(index);
	if (!cmnt.inited)
		return false;
	return cmnt.inited;
}

function getComment(index) {
	if (!markit_comments.get("" + index))
		return markit_oldComments.get("" + index);
	return markit_comments.get("" + index);
}

function changeToSpan(index) {
	var tc = document.getElementById("innerDivComment" + index);
	var ta = tc.lastChild;
	var comment = ta.value;
	var sc = document.createElement("div");
	sc.setAttribute("class", "contentComment");
	sc.setAttribute("onClick", "changeToTa(" + index + ")");
	sc.innerHTML = comment;
	tc.replaceChild(sc, ta);
	var cmnt = markit_comments.get("" + index);
	if (!cmnt) {
		cmnt = markit_oldComments.get("" + index);
		if (cmnt.cmnt != comment) {
			cmnt.status = 2;
			markit_comments.put("" + index, cmnt);
		}
	}
	cmnt.cmnt = comment;
}

function changeToTa(index) {
	var tc = document.getElementById("innerDivComment" + index);
	var sc = tc.lastChild;
	var comment = sc.innerHTML;
	var ta = document.createElement("textArea");
	ta.setAttribute("class", "contentComment");
	ta.setAttribute("onBlur", "changeToSpan(" + index + ")");
	ta.value = comment;
	tc.replaceChild(ta, sc);
}

function init(index) {
	var cmnt = getComment(index);
	var cp = document.createElement("div");
	cp.setAttribute("id", "outDivComment" + index);
	cp.setAttribute("class", "outDivComment");
	cp.innerHTML = "<div class='innerDivComment' id='innerDivComment" + index
			+ "'>" + "<span class='panelLogo'></span>"
			+ "<span class='panelTitle'>" + cmnt.usr + "</span>"
			+ "<span class='panelCtrlBtn' onclick='hideComment(" + index
			+ ")'></span><br>" + "<span class='panelDate'>" + cmnt.time
			+ "</span><br><hr>"
			+ "<textArea class='contentComment' onblur='changeToSpan(" + index
			+ ")'>" + cmnt.cmnt + "</textArea>" + "</div>";
	cp.style.top = getCommentIconY("iconComment" + index) + "px";
	cp.style.left = getCommentIconX("iconComment" + index) + "px";
	cp.style.display = "block";
	drag(cp, 0);
	cmnt.inited = true;
	document.body.appendChild(cp);
}

function HashTable() {
	this.container = new Object();
	/** put element */
	this.put = function(key, value) {
		if (typeof (key) == "undefined") {
			return false;
		}
		if (this.contains(key)) {
			return false;
		}
		this.container[key] = typeof (value) == "undefined" ? null : value;
		return true;
	};
	/** remove element */
	this.remove = function(key) {
		delete this.container[key];
	};
	/** get size */
	this.size = function() {
		var size = 0;
		for ( var attr in this.container) {
			size++;
		}
		return size;
	};
	/** get value by key */
	this.get = function(key) {
		return this.container[key];
	};
	/** containts a key */
	this.contains = function(key) {
		return typeof (this.container[key]) != "undefined";
	};
	/** clear all entrys */
	this.clear = function() {
		for ( var attr in this.container) {
			delete this.container[attr];
		}
	};
	this.addAll = function(ht) {
		for ( var key in ht.container) {
			if (this.contains(key)) {
				alert("!!!CONFLICT!!!");
			}
			this.container[key] = ht.container[key];
		}
	};
	/** hashTable 2 string */
	this.toString = function() {
		return JSON.stringify(this.container);
	};
}
